import FragmentManager, { AddListener, RemoveListener } from "../FragmentManager";
import { Node, } from "cc";
import View from "../view/View";
import { PopUpFragment } from "./PopUpFragment";
export class DialogFragment extends PopUpFragment {

    private mActive = false;
    private mWaitView: { view: View, listener?: AddListener, params?: any }[] = []

    async addView(manager: FragmentManager, view: View, listener?: AddListener, params?: any): Promise<Node | null> {
        if (this.mActive) {
            this.mWaitView.push({
                view: view, listener: listener, params: params
            })
            return Promise.resolve(null);
        }
        const result = await super.addView(manager, view, listener, params);
        this.mActive = result != null;
        return Promise.resolve(result);
    }

    async removeView(manager: FragmentManager, view: View, destroy?: boolean, listener?: RemoveListener, params?: any): Promise<boolean> {
        const that = this;
        const proxyListener: RemoveListener = {
            onRemove(manager, view, node, destroy) {
                listener?.onRemove?.(manager, view, node, destroy);
                that.mActive = false;
                const nextView = that.mWaitView.shift();
                if (nextView) {
                    that.addView(manager, nextView.view, nextView.listener, nextView.params);
                }
            },
        }
        return super.removeView(manager, view, destroy, proxyListener, params);
    }

    clear(manager: FragmentManager, destroy: boolean): void {


        super.clear(manager, destroy)
    }

}